import typescriptEslint from "@typescript-eslint/eslint-plugin";
import tsParser from "@typescript-eslint/parser";

export default [
    {
        files: ["**/*.ts"],
        plugins: {
            "@typescript-eslint": typescriptEslint,
        },
        languageOptions: {
            parser: tsParser,
            ecmaVersion: 2022,
            sourceType: "module",
            parserOptions: {
                project: "./tsconfig.json",
            },
        },
        rules: {
            // TypeScript 特定规则
            "@typescript-eslint/naming-convention": [
                "warn",
                {
                    selector: "import",
                    format: ["camelCase", "PascalCase"],
                },
                {
                    selector: "variableLike",
                    format: ["camelCase"],
                },
                {
                    selector: "typeLike",
                    format: ["PascalCase"],
                },
                {
                    selector: "property",
                    format: ["camelCase"],
                    filter: {
                        regex: "^(__.*__|[A-Z_]+)$",
                        match: false,
                    },
                },
            ],
            "@typescript-eslint/no-unused-vars": [
                "error",
                {
                    argsIgnorePattern: "^_",
                    varsIgnorePattern: "^_",
                },
            ],
            "@typescript-eslint/explicit-function-return-type": "warn",
            "@typescript-eslint/no-explicit-any": "warn",
            "@typescript-eslint/prefer-nullish-coalescing": "error",
            "@typescript-eslint/prefer-optional-chain": "error",
            "@typescript-eslint/no-non-null-assertion": "warn",
            "@typescript-eslint/no-floating-promises": "error",
            "@typescript-eslint/await-thenable": "error",
            "@typescript-eslint/no-misused-promises": "error",
            "@typescript-eslint/prefer-readonly": "warn",
            "@typescript-eslint/prefer-string-starts-ends-with": "error",

            // 通用规则
            "curly": "error",
            "eqeqeq": ["error", "always"],
            "no-throw-literal": "error",
            "semi": ["error", "always"],
            "quotes": ["error", "single", { avoidEscape: true }],
            "indent": ["error", 4, { SwitchCase: 1 }],
            "comma-dangle": ["error", "never"],
            "no-trailing-spaces": "error",
            "no-multiple-empty-lines": ["error", { max: 2, maxEOF: 1 }],
            "object-curly-spacing": ["error", "always"],
            "array-bracket-spacing": ["error", "never"],
            "key-spacing": ["error", { beforeColon: false, afterColon: true }],
            "space-before-function-paren": ["error", "never"],
            "space-in-parens": ["error", "never"],
            "space-before-blocks": "error",
            "keyword-spacing": "error",
            "no-console": "warn",
            "prefer-const": "error",
            "no-var": "error",
            "no-duplicate-imports": "error",
            "no-unused-expressions": "error",
            "consistent-return": "error",
            "default-case": "error",
            "no-fallthrough": "error",
            "no-empty": "error",
            "no-empty-function": "warn",
            "no-magic-numbers": [
                "warn",
                {
                    ignore: [-1, 0, 1, 2],
                    ignoreArrayIndexes: true,
                    ignoreDefaultValues: true,
                },
            ],
        },
    },
    {
        files: ["**/*.test.ts", "**/*.spec.ts"],
        rules: {
            // 测试文件中放宽一些规则
            "no-magic-numbers": "off",
            "@typescript-eslint/no-explicit-any": "off",
            "no-console": "off",
        },
    },
];